import { RequestMethod } from '@nestjs/common';
import { Request } from 'express';
import { User } from 'libs/db/entity/user.entity';
import { Column, CreateDateColumn, DeleteDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { RequestMessage } from './about/request-message';
import { ActorEnum } from './enum/actor.enum';
import { HandleMoudle } from './enum/handle-module.enum';
import { HandleStatus } from './enum/handle-status.enum';
import { Villager } from './villager.entity';

@Entity()
export class Logger {

    constructor(req: Request | any, handleMethod: string, handleName: string, handleStatus: HandleStatus, handleMoudle: HandleMoudle, actor: ActorEnum) {
        if(actor == ActorEnum.villager) {
            this.villager = req && req.user && req.user.id
        } else if (actor == ActorEnum.user) {
            this.user = req && req.user && req.user.id
        }
        
        this.actor = actor

        let reqMessage: RequestMessage = new RequestMessage()
        reqMessage.body = req && req.body
        reqMessage.method = req && req.method as unknown as RequestMethod
        reqMessage.param = req && req.params
        reqMessage.path = req && req.path
        reqMessage.query = req && req.query
        this.reqMessage = reqMessage

        this.handleMethod = handleMethod
        this.handleName = handleName
        this.handleStatus = handleStatus
        this.handleMoudle = handleMoudle
    }

    

    @PrimaryGeneratedColumn()
    id: number;

    @ManyToOne(() => User, User => User.logs)
    user: number | User | any;

    @ManyToOne(() => Villager, Villager => Villager.logs)
    villager: number | Villager | any;
    

    @Column({
        type: "simple-json",
    })
    reqMessage: RequestMessage

    @Column()
    handleMethod: string

    @Column({
        type: "enum",
        enum: HandleMoudle
    })
    handleMoudle: HandleMoudle

    @Column({
        type: "enum",
        enum: HandleStatus
    })
    handleStatus: HandleStatus

    @Column()
    handleName: string

    @Column({
        type: "enum",
        enum: ActorEnum
    })
    actor: ActorEnum
    

    @CreateDateColumn({ type: 'timestamp' })
    createdAt?: Date;

    @DeleteDateColumn({ type: 'timestamp' })
    deleteAt: Date
}